package com.goldplusgold.tradedelay.app.application;

import android.os.Handler;
import android.text.TextUtils;

import com.goldplusgold.database.lib.DatabaseCache;
import com.goldplusgold.network.lib.AppCollectedInfoUtil;
import com.goldplusgold.network.lib.http.netty.ChannelBootstrapManager;
import com.example.account.manager.AppCacheSharedPreferences;
import com.goldplusgold.support.lib.utils.AppInfoUtils;
import com.goldplusgold.support.lib.wxapi.WeChatUtil;
import com.goldplusgold.tradedelay.app.market.channel.MarketClientBootstrap;
import com.goldplusgold.tradedelay.app.my.account.UserInfo;
import com.goldplusgold.tradedelay.app.my.networkservice.MyService;

/**
 * Created by Administrator on 2017/7/25.
 */

 class TDInitApplication {

    TDApplication tdApplication;
    public TDInitApplication(TDApplication tdApplication){
        this.tdApplication = tdApplication;
    }

    public void init(){
        nowInit();
        delayInit();
    }

    private void nowInit(){
        DatabaseCache.getInstance().initContext(tdApplication);
        ChannelBootstrapManager.getInstance().register(MarketClientBootstrap.class.getName(),new MarketClientBootstrap());
    }

    private void delayInit(){
       /* new Handler().postDelayed( new Runnable() {
            @Override
            public void run() {
                weChatService();
                initAppInfo();
                AppCacheSharedPreferences.init(tdApplication);

            }
        },500);*/
        weChatService();
        initAppInfo();
        AppCacheSharedPreferences.init(tdApplication);
    }

    private void weChatService() {
        WeChatUtil.getInstance().regToWX(tdApplication);
    }

    private void initAppInfo() {
        final String versionName = AppInfoUtils.getVersionName(tdApplication);
        String token = MyService.getInstance().queryToken();
        if (!TextUtils.isEmpty(token)) {
            String time = String.valueOf(System.currentTimeMillis());
            AppCollectedInfoUtil.initAppCollectedInfo(tdApplication, token, time, versionName);
        }
        UserInfo userInfo = MyService.getInstance().queryUserInfo();
        MyService.getInstance().setUserInfo(userInfo);
    }

}
